#include "WirteAndSave.h"

WirteAndSave::WirteAndSave()
{}

void haffman::write(QString path) // 缓冲区写入文件
{
  FILE *tem;
  char  s[100];
  int   num2 = path.length();
  for (int i = num2 - 1; i >= 0; i--)
  {
    if (path[i] == '.') { num2 = i; break; }
  }
  formulate = path.right(path.length() - num2); // 截取原来的文件格式保存起来
  path      = path.left(num2);
  path     += ".zip";
  strcpy(s,               path.toLocal8Bit().data());
  int num = 0;
  strcpy(code_name,       s);
  while (code_name[num] != '.' && code_name[num]) ++num;
  strcpy(code_name + num, ".cod");

  tem = fopen(s, "wb");
  if (tem == 0)
  {
    return;
  }
  for (int i = 0; i <= max_bu; ++i)
  {
    fputc(buffer[i], tem);
  }
  fclose(tem);
  QFile tem1(path);
  final_size = tem1.size();
}

void haffman::savecode()
{
  FILE *tem;
  tem = fopen(code_name, "w");
  if (tem == 0)
  {
    return;
  }
  char s1[10];
  strcpy(s1, formulate.toLatin1().data());
  fprintf(tem, "%s\n",    s1);         // 保存文件格式
  fprintf(tem, "%d %d\n", max_bu, bu); // 保存缓冲区大小
  for (int i = 0; i < bas_len; ++i)    // 保存字符 编码 编码长度
  {
    fprintf(tem, "%d %d %d\n", bas[i]->data, bas[i]->code, bas[i]->code_len);
  }
  fclose(tem);
}
